package com.newhonor.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.newhonor.server.mapper.DepartmentMapper;
import com.newhonor.server.pojo.Department;
import com.newhonor.server.pojo.ResBean;
import com.newhonor.server.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author newhonor
 * @Date 2021/2/23 11:03
 * @Version 1.0
 **/
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public List<Department> getAllDepartments() {
        return departmentMapper.getAllDepartments(-1);
    }

    @Override
    public ResBean addDep(Department department) {
        department.setEnabled(true);
        departmentMapper.addDep(department);
        if(1 == department.getResult()){
            return ResBean.success("添加成功",department);
        }
        return ResBean.error("添加失败");
    }

    @Override
    public ResBean deleteDep(Integer id) {
        //获取存储过程的out参数
        Department department = new Department();
        department.setId(id);
        departmentMapper.deleteDep(department);
        if(-2 == department.getResult()){
            return ResBean.error("该部门下有子部门，删除失败");
        }
        if(-1 == department.getResult()){
            return ResBean.error("该部门还有员工，删除失败");
        }
        if(1 == department.getResult()){
            return ResBean.success("删除成功");
        }
        return ResBean.error("删除失败");
    }
}
